<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="24" :xs="24">
        <!-- 数据筛选表单 -->
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="100px"
        >
          <!-- 数据筛选表单组件 -->
          <!-- <el-form-item label="房型ID" prop="typeId">
            <el-input
              v-model="queryParams.typeId"
              placeholder="请输入房型ID"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item> -->

          <el-form-item label="房型名称" prop="typeId">
            <el-select v-model="queryParams.typeId" placeholder="请选择房型">
              <el-option
                v-for="item in typeOptions"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeId"
                @keyup.enter.native="handleQuery"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- 
          <el-form-item label="床位数" prop="typeId">
            <el-select v-model="queryParams.typeId" placeholder="请选择床位数">
              <el-option
                v-for="item in typeOptions"
                :key="item.typeId"
                :label="item.bed"
                :value="item.typeId"
                @keyup.enter.native="handleQuery"
              >
              </el-option>
            </el-select>
          </el-form-item> -->

          <el-form-item label="房间号" prop="roomNo">
            <el-input
              v-model="queryParams.roomNo"
              placeholder="请输入房间号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <!-- <el-form-item label="状态" prop="roomStatus">
            <el-input
              v-model="queryParams.roomStatus"
              placeholder="请输入状态"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item> -->

          <el-form-item label="状态" prop="roomStatus">
            <el-select
              v-model="queryParams.roomStatus"
              placeholder="请选择状态"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.statusId"
                :label="item.statusName"
                :value="item.statusId"
                @keyup.enter.native="handleQuery"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <!-- 
          <el-form-item label="楼层" prop="roomFloor">
            <el-input
              v-model="queryParams.roomFloor"
              placeholder="请输入楼层"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item> -->

          <el-form-item label="楼层" prop="roomFloor">
            <el-select v-model="queryParams.roomFloor" placeholder="请选择楼层">
              <el-option
                v-for="item in floorOptions"
                :key="item.floorId"
                :label="item.floorName"
                :value="item.floorId"
                @keyup.enter.native="handleQuery"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <!-- 数据筛选操作按钮 -->
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置
            </el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="3">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
              >新增房间
            </el-button>
          </el-col>
        </el-row>

        <!-- 数据表格 -->
        <el-table v-loading="loading" :data="roomList">
          <!-- 数据表格列 -->
          <el-table-column label="房间ID" align="center" prop="roomId" />
          <el-table-column label="房型ID" align="center" prop="typeId" />
          <el-table-column label="房间号" align="center" prop="roomNo" />
          <el-table-column label="状态" align="center" prop="roomStatus" />
          <el-table-column label="楼层" align="center" prop="roomFloor" />
          <el-table-column
            label="房型名称"
            align="center"
            prop="hotelType.typeName"
          />
          <el-table-column label="床位数" align="center" prop="hotelType.bed" />
          <el-table-column label="价钱" align="center" prop="hotelType.price" />
          <!-- 数据行操作按钮 -->
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改
              </el-button>
              <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 数据分页加载 -->
        <el-pagination
          :current-page="pageNum"
          :page-sizes="[5, 10, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加修改room对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- 数据编辑表单组件 -->
        <!-- <el-form-item label="房型ID" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入房型ID" />
        </el-form-item> -->

        <el-form-item label="房型名称" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择房型">
            <el-option
              v-for="item in typeOptions"
              :key="item.typeId"
              :label="item.typeName"
              :value="item.typeId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="床位数" prop="hotelType.bed">
          <el-select v-model="form.HotelType.bed" placeholder="请选择床位数">
            <el-option
              v-for="item in typeOptions"
              :key="item.typeId"
              :label="item.bed"
              :value="item.typeId"
            >
            </el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item label="房间号" prop="roomNo">
          <el-input v-model="form.roomNo" placeholder="请输入房间号" />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="roomStatus">
          <el-input v-model="form.roomStatus" placeholder="请输入状态" />
        </el-form-item> -->

        <el-form-item label="状态" prop="roomStatus">
          <el-select v-model="form.roomStatus" placeholder="请选择状态">
            <el-option
              v-for="item in statusOptions"
              :key="item.statusId"
              :label="item.statusName"
              :value="item.statusId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="楼层" prop="roomFloor">
          <el-input v-model="form.roomFloor" placeholder="请输入楼层" />
        </el-form-item> -->

        <el-form-item label="楼层" prop="roomFloor">
          <el-select v-model="form.roomFloor" placeholder="请选择楼层">
            <el-option
              v-for="item in floorOptions"
              :key="item.floorId"
              :label="item.floorName"
              :value="item.floorId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 数据提交操作按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
/** 导入axios Api */
import { listType } from "@/api/type";
import {
  listRoomByPage,
  getRoom,
  addRoom,
  updateRoom,
  deleteRoom,
} from "@/api/room";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Room",
  data() {
    return {
      //Room状态
      statusOptions: [
        {
          statusId: "空闲",
          statusName: "空闲",
        },
        {
          statusId: "占用",
          statusName: "占用",
        },
        {
          statusId: "打扫",
          statusName: "打扫",
        },
      ],
      //Room楼层
      floorOptions: [
        {
          floorId: 1,
          floorName: "1",
        },
        {
          floorId: 2,
          floorName: "2",
        },
        {
          floorId: 3,
          floorName: "3",
        },
        {
          floorId: 4,
          floorName: "4",
        },
      ],

      // Room类型
      typeOptions: undefined,
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 当前页
      pageNum: 0,
      // 每页数据量
      pageSize: 0,
      // Room表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        roomId: undefined,
        typeId: undefined,
        roomNo: undefined,
        roomStatus: undefined,
        roomFloor: undefined,
        typeRank: undefined,
      },
      // 数据编辑表单
      form: {},
      // 表单数据校验规则
      rules: {
        roomId: [
          { required: true, message: "房间ID不能为空", trigger: "blur" },
        ],
        typeId: [
          { required: true, message: "房型ID不能为空", trigger: "blur" },
        ],
        roomNo: [
          { required: true, message: "房间号不能为空", trigger: "blur" },
        ],
        roomStatus: [
          { required: true, message: "状态不能为空", trigger: "blur" },
        ],
        roomFloor: [
          { required: true, message: "楼层不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    //查询所有type，赋值typeOptions
    listType().then((response) => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** 查询Room数据列表 */
    getList() {
      this.loading = true;
      listRoomByPage(this.queryParams).then((response) => {
        this.roomList = response.data.list;
        this.total = response.data.total;
        this.pageNum = response.data.pageNum;
        this.pageSize = response.data.pageSize;
        this.loading = false;
      });
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },

    /** 数据编辑对话框取消按钮 */
    cancel() {
      this.open = false;
      this.resetForm();
    },

    /** 数据编辑表单清空 */
    resetForm() {
      this.form = {
        roomId: undefined,
        typeId: undefined,
        roomNo: undefined,
        roomStatus: undefined,
        roomFloor: undefined,
      };
    },

    /** 数据筛选参数重置按钮 */
    resetQueryParams() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 5;
    },

    /** 数据筛选搜索按钮 */
    handleQuery() {
      this.resetQueryParams();
      this.getList();
      this.$message.success("查询成功");
    },

    /** 数据筛选重置按钮 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 5,
        roomId: undefined,
        typeId: undefined,
        roomNo: undefined,
        roomStatus: undefined,
        roomFloor: undefined,
      };
      this.getList();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm();
      this.open = true;
      this.title = "添加房间";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm();
      getRoom(row.roomId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房间";
      });
    },

    /** 数据提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title === "修改房间") {
            updateRoom(this.form).then((response) => {
              this.open = false;
              this.title = "";
              this.getList();
              this.$message.success(response.data);
            });
          } else {
            addRoom(this.form).then((response) => {
              this.open = false;
              this.title = "";
              this.getList();
              this.$message.success(response.data);
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.roomNo + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteRoom(row.roomId);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
